﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Wmi
{
    namespace Dfsr
    {
        public enum MonitorStatus {Success,GenericDBError,IdRecordNotFound,VolumeNotFound,AccessDenied,GenericError,}
        public enum Fence {Unfence,InitialSync,InitialPrimary,Default,Fence}

        public class DfsrException : Exception
        {
            private String[] monitorStatusErrorMsg = { 
                                                          "Success.", 
                                                          "Generic database error.", 
                                                          "ID record not found.",
                                                          "Volume not found.",
                                                          "Access denied.",
                                                          "Generic error." };


            public DfsrException(UInt32 error)
            {
                Type typeMonitorStatus = typeof(MonitorStatus);
                String monitorStatusError = Enum.GetName(typeMonitorStatus, error);
                
            }
        }
        /// <summary>
        /// The DFSR monitoring provider class. 
        /// This singleton class has provider-specific settings in addition 
        /// to computer-wide service properties and methods.
        /// </summary>
        public class Info
        {
            public enum State
            {
                ServiceStarting,
                ServiceRunning,
                ServiceDegraded,
                ServiceShuttingDown,
            }
            private System.Management.ManagementScope managementScope;
            private String queryDfsrInfo = "SELECT * FROM DfsrInfo";
            private System.Collections.ArrayList arrayDfsrInfo;
            private System.Management.SelectQuery sqDfsrInfo;
            private System.Management.ManagementObjectSearcher mosDfsrInfo;
            private System.Management.ManagementObjectCollection mocDfsrInfo;

            public struct DfsrInfo
            {
                public String ProviderVersion;
                public String ServiceStartTime;
                public String LastDsPollTime;
                public Byte State;
                public UInt32 LastErrorCode;
                public UInt32 LastErrorMessageId;
            }

            public Info(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqDfsrInfo = new System.Management.SelectQuery(queryDfsrInfo);
                mosDfsrInfo = new ManagementObjectSearcher(managementScope, sqDfsrInfo);
                mocDfsrInfo = mosDfsrInfo.Get();
            }
            public void Get()
            {
                arrayDfsrInfo = new System.Collections.ArrayList();
                DfsrInfo _DfsrInfo = new DfsrInfo();
                try
                {
                    foreach (ManagementObject moDfsrInfo in mocDfsrInfo)
                    {
                        if (moDfsrInfo["ProviderVersion"] != null)
                        {
                            _DfsrInfo.ProviderVersion = (String)moDfsrInfo["ProviderVersion"];
                        }
                        if (moDfsrInfo["ServiceStartTime"] != null)
                        {
                            _DfsrInfo.ServiceStartTime = (String)moDfsrInfo["ServiceStartTime"];
                        }
                        if (moDfsrInfo["LastDsPollTime"] != null)
                        {
                            _DfsrInfo.LastDsPollTime = (String)moDfsrInfo["LastDsPollTime"];
                        }
                        if (moDfsrInfo["State"] != null)
                        {
                            _DfsrInfo.State = (Byte)moDfsrInfo["State"];
                        }
                        if (moDfsrInfo["LastErrorCode"] != null)
                        {
                            _DfsrInfo.LastErrorCode = (UInt32)moDfsrInfo["LastErrorCode"];
                        }
                        if (moDfsrInfo["LastErrorMessageId"] != null)
                        {
                            _DfsrInfo.LastErrorMessageId = (UInt32)moDfsrInfo["LastErrorMessageId"];
                        }
                        arrayDfsrInfo.Add(_DfsrInfo);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }
            }
        }
        /// <summary>
        /// This class provides statistical and operational information for active synchronization 
        /// sessions between the local replication group member and one of its partners.
        /// </summary>
        public class SyncInfo
        {
            public enum InitiationReason { Schedule, Forced, Paused, ForcedUntilSync, };
            public enum State { Initialized, Connecting, InProgress, Completed, InSync, Interrupted, InError, };
            
            private System.Management.ManagementScope managementScope;
            private System.Collections.ArrayList arrayDfsrSyncInfo;
            private System.Management.SelectQuery sqDfsrSyncInfo;
            private String queryDfsrSyncInfo = "SELECT * FROM DfsrSyncInfo";
            private System.Management.ManagementObjectSearcher mosDfsrSyncInfo;
            private System.Management.ManagementObjectCollection mocDfsrSyncInfo;

            public struct DfsrSyncInfo
            {
                public String SyncGuid;
                public String ConnectionGuid;
                public String MemberGuid;
                public String MemberName;
                public String PartnerGuid;
                public String PartnerName;
                public String ReplicationGroupGuid;
                public String ReplicationGroupName;
                public Boolean Inbound;
                public UInt32 State;
                public UInt32 InitiationReason;
                public String StartTime;
                public String EndTime;
                public UInt32 UpdatesTransferred;
                public UInt64 BytesTransferred;
                public UInt32 UpdatesNotTransferred;
                public UInt32 UpdatesToBeTransferred;
                public UInt32 ConflictsGenerated;
                public UInt32 TombstonesGenerated;
                public UInt32 LastErrorCode;
                public UInt32 LastErrorMessageId;
                public String ForceReplicationEndTime;
                public UInt32 ForceReplicationBandwidthlevel;
            }
            public SyncInfo(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqDfsrSyncInfo = new System.Management.SelectQuery(queryDfsrSyncInfo);
                mosDfsrSyncInfo = new ManagementObjectSearcher(managementScope, sqDfsrSyncInfo);
                mocDfsrSyncInfo = mosDfsrSyncInfo.Get();
            }
            public void Get()
            {
                arrayDfsrSyncInfo = new System.Collections.ArrayList();
                DfsrSyncInfo _DfsrSyncInfo = new DfsrSyncInfo();
                try
                {
                    foreach (ManagementObject moDfsrSyncInfo in mocDfsrSyncInfo)
                    {
                        if (moDfsrSyncInfo["SyncGuid"] != null)
                        {
                            _DfsrSyncInfo.SyncGuid = (String)moDfsrSyncInfo["SyncGuid"];
                        }
                        if (moDfsrSyncInfo["ConnectionGuid"] != null)
                        {
                            _DfsrSyncInfo.ConnectionGuid = (String)moDfsrSyncInfo["ConnectionGuid"];
                        }
                        if (moDfsrSyncInfo["MemberGuid"] != null)
                        {
                            _DfsrSyncInfo.MemberGuid = (String)moDfsrSyncInfo["MemberGuid"];
                        }
                        if (moDfsrSyncInfo["MemberName"] != null)
                        {
                            _DfsrSyncInfo.MemberName = (String)moDfsrSyncInfo["MemberName"];
                        }
                        if (moDfsrSyncInfo["PartnerGuid"] != null)
                        {
                            _DfsrSyncInfo.PartnerGuid = (String)moDfsrSyncInfo["PartnerGuid"];
                        }
                        if (moDfsrSyncInfo["PartnerName"] != null)
                        {
                            _DfsrSyncInfo.PartnerName = (String)moDfsrSyncInfo["PartnerName"];
                        }
                        if (moDfsrSyncInfo["ReplicationGroupGuid"] != null)
                        {
                            _DfsrSyncInfo.ReplicationGroupGuid = (String)moDfsrSyncInfo["ReplicationGroupGuid"];
                        }
                        if (moDfsrSyncInfo["ReplicationGroupName"] != null)
                        {
                            _DfsrSyncInfo.ReplicationGroupName = (String)moDfsrSyncInfo["ReplicationGroupName"];
                        }
                        if (moDfsrSyncInfo["Inbound"] != null)
                        {
                            _DfsrSyncInfo.Inbound = (Boolean)moDfsrSyncInfo["Inbound"];
                        }
                        if (moDfsrSyncInfo["State"] != null)
                        {
                            _DfsrSyncInfo.State = (UInt32)moDfsrSyncInfo["State"];
                        }
                        if (moDfsrSyncInfo["InitiationReason"] != null)
                        {
                            _DfsrSyncInfo.InitiationReason = (UInt32)moDfsrSyncInfo["InitiationReason"];
                        }
                        if (moDfsrSyncInfo["StartTime"] != null)
                        {
                            _DfsrSyncInfo.StartTime = (String)moDfsrSyncInfo["StartTime"];
                        }
                        if (moDfsrSyncInfo["EndTime"] != null)
                        {
                            _DfsrSyncInfo.EndTime = (String)moDfsrSyncInfo["EndTime"];
                        }
                        if (moDfsrSyncInfo["UpdatesTransferred"] != null)
                        {
                            _DfsrSyncInfo.UpdatesTransferred = (UInt32)moDfsrSyncInfo["UpdatesTransferred"];
                        }
                        if (moDfsrSyncInfo["BytesTransferred"] != null)
                        {
                            _DfsrSyncInfo.BytesTransferred = (UInt64)moDfsrSyncInfo["BytesTransferred"];
                        }
                        if (moDfsrSyncInfo["UpdatesNotTransferred"] != null)
                        {
                            _DfsrSyncInfo.UpdatesNotTransferred = (UInt32)moDfsrSyncInfo["UpdatesNotTransferred"];
                        }
                        if (moDfsrSyncInfo["UpdatesToBeTransferred"] != null)
                        {
                            _DfsrSyncInfo.UpdatesToBeTransferred = (UInt32)moDfsrSyncInfo["UpdatesToBeTransferred"];
                        }
                        if (moDfsrSyncInfo["ConflictsGenerated"] != null)
                        {
                            _DfsrSyncInfo.ConflictsGenerated = (UInt32)moDfsrSyncInfo["ConflictsGenerated"];
                        }
                        if (moDfsrSyncInfo["TombstonesGenerated"] != null)
                        {
                            _DfsrSyncInfo.TombstonesGenerated = (UInt32)moDfsrSyncInfo["TombstonesGenerated"];
                        }
                        if (moDfsrSyncInfo["LastErrorCode"] != null)
                        {
                            _DfsrSyncInfo.LastErrorCode = (UInt32)moDfsrSyncInfo["LastErrorCode"];
                        }
                        if (moDfsrSyncInfo["LastErrorMessageId"] != null)
                        {
                            _DfsrSyncInfo.LastErrorMessageId = (UInt32)moDfsrSyncInfo["LastErrorMessageId"];
                        }
                        /*if (moDfsrSyncInfo["ForceReplicationEndTime"] != null)
                        {
                            _DfsrSyncInfo.ForceReplicationEndTime = (String)moDfsrSyncInfo["ForceReplicationEndTime"];
                        }*/
                        /*if (moDfsrSyncInfo["ForceReplicationBandwidthlevel"] != null)
                        {
                            _DfsrSyncInfo.ForceReplicationBandwidthlevel = (UInt32)moDfsrSyncInfo["ForceReplicationBandwidthlevel"];
                        }*/

                        arrayDfsrSyncInfo.Add(_DfsrSyncInfo);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    //System.Windows.Forms.MessageBox.Show( error );
                    Console.Error.WriteLine(error);
                }
            }

        }
        /// <summary>
        /// This class provides statistical and operational information for each 
        /// incoming and outgoing connection of the local replication group members.
        /// </summary> 
        public class ConnectionInfo
        {
            public enum State
            {
                Connecting,
                Online,
                Offline,
                InError,
            }
            public enum Mode
            {
                ObeyConfiguredSchedule = 1,
                SyncNow,
                StopSync,
                SyncUntilInSync,
            }
            public enum Bandwidth
            {
                LevelOff = 0x0,
                Kbps16,
                Kbps64,
                Kbps128,
                Kbps256,
                Kbps512,
                Mbps1,
                Mbps2,
                Mbps4,
                Mbps8,
                Mbps16,
                Mbps32,
                Mbps64,
                Mbps128,
                Mbps256,
                Infinite,
            }
            private String queryDfsrConnectionInfo = "SELECT * FROM DfsrConnectionInfo";
            private System.Collections.ArrayList arrayDfsrConnectionInfo;
            private System.Management.SelectQuery sqDfsrConnectionInfo;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosDfsrConnectionInfo;
            private System.Management.ManagementObjectCollection mocDfsrConnectionInfo;
            public struct DfsrConnectionInfo
            {
                String ConnectionGuid;
                String MemberGuid;
                String MemberName;
                String PartnerGuid;
                String PartnerName;
                String ReplicationGroupGuid;
                String ReplicationGroupName;
                Boolean Inbound;
                UInt32 State;
                String LastSyncTime;
                UInt32 LastSyncDuration;
                String LastSuccessfulSyncTime;
                String NextSyncTime;
                UInt32 LastErrorCode;
                UInt32 LastErrorMessageId;
            }
            public ConnectionInfo(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqDfsrConnectionInfo = new System.Management.SelectQuery(queryDfsrConnectionInfo);
                mosDfsrConnectionInfo = new ManagementObjectSearcher(managementScope, sqDfsrConnectionInfo);
                mocDfsrConnectionInfo = mosDfsrConnectionInfo.Get();
            }
            /// <summary>
            /// Controls replication by either forcing or suspending replication. 
            /// DFSR will revert to the normal schedule at the end of the specified duration.
            /// </summary>
            /// <param name="Mode"></param>
            /// <param name="DurationInMin"></param>
            /// <param name="Bandwidth"></param>
            /// <returns>Boolean</returns>
            public Boolean ForceReplication(UInt32 Mode, UInt32 DurationInMin, UInt32 Bandwidth )
            {
                Object returnObj = null;
                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocDfsrConnectionInfo)
                    {
                        returnObj = moDfsrConnectionInfo.InvokeMethod("ForceReplication", new Object[] { Mode, DurationInMin, Bandwidth });
                        if ((UInt32)returnObj != 0)
                        {
                            throw new DfsrException((UInt32)returnObj);
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }
                return true;
            }
            /// <summary>
            /// Controls replication by forcing a designated folder to replicate. 
            /// After the duration expires, the DFS Replication service will revert to the configured replication schedule.
            /// </summary>
            /// <param name="DurationInMin"></param>
            /// <param name="Bandwidth"></param>
            /// <param name="ReplicatedFolderGuid"></param>
            /// <returns></returns>
            public Boolean ForceFolderReplication(UInt32 DurationInMin, UInt32 Bandwidth, String ReplicatedFolderGuid)
            {
                Object returnObj = null;
                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocDfsrConnectionInfo)
                    {
                        returnObj = moDfsrConnectionInfo.InvokeMethod("ForceFolderReplication", new Object[] { DurationInMin, Bandwidth, ReplicatedFolderGuid });
                        if ((UInt32)returnObj != 0)
                        {
                            throw new DfsrException((UInt32)returnObj);
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }

                return true;
            }
            /// <summary>
            /// Forces a download of a specified resource. 
            /// The connection and bandwidth are controlled by other methods. 
            /// This method assumes that the connection is on. 
            /// The unique ID of the resource to be downloaded can be obtained by querying the ID record.
            /// </summary>
            /// <param name="ReplicatedFolderGuid"></param>
            /// <param name="Uid"></param>
            /// <returns></returns>
            public Boolean ForceDownload(String ReplicatedFolderGuid, string Uid)
            {
                Object returnObj = null;
                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocDfsrConnectionInfo)
                    {
                        returnObj = moDfsrConnectionInfo.InvokeMethod("ForceDownload", new Object[] { ReplicatedFolderGuid, Uid });
                        if ((UInt32)returnObj != 0)
                        {
                            throw new DfsrException((UInt32)returnObj);
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }

                return true;
            }
            /// <summary>
            /// Checks connectivity with the partner's computer.
            /// </summary>
            /// <returns></returns>
            public Boolean CheckConnectivity()
            {
                Object returnObj = null;
                try
                {
                    foreach (ManagementObject moDfsrConnectionInfo in mocDfsrConnectionInfo)
                    {
                        returnObj = moDfsrConnectionInfo.InvokeMethod("CheckConnectivity", null);
                        if ((UInt32)returnObj != 0)
                        {
                            throw new DfsrException((UInt32)returnObj);
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }

                return true;
            }

        }
        /// <summary>
        /// Provides statistical and operational information for each replicated folder hosted on the local computer.
        /// </summary>
        public class ReplicatedFolderInfo
        {
            private String queryDfsrReplicatedFolderInfo = "SELECT * FROM DfsrReplicatedFolderInfo";
            private System.Collections.ArrayList arrayDfsrReplicatedFolderInfo;
            private System.Management.SelectQuery sqDfsrReplicatedFolderInfo;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosDfsrReplicatedFolderInfo;
            private System.Management.ManagementObjectCollection mocDfsrReplicatedFolderInfo;

            public struct DfsrReplicatedFolderInfo
            {
                public String ReplicatedFolderGuid;
                public String ReplicatedFolderName;
                public String ReplicationGroupGuid;
                public String ReplicationGroupName;
                public String MemberGuid;
                public String MemberName;
                public String LastConflictCleanupTime;
                public String LastTombstoneCleanupTime;
                public UInt32 CurrentStageSizeInMb;
                public UInt32 CurrentConflictSizeInMb;
                public Byte State;
                public UInt32 LastErrorCode;
                public UInt32 LastErrorMessageId;
            }
            public ReplicatedFolderInfo(System.Management.ManagementScope managementScope)
            {   
                this.managementScope = managementScope;
                sqDfsrReplicatedFolderInfo = new System.Management.SelectQuery(queryDfsrReplicatedFolderInfo);
                mosDfsrReplicatedFolderInfo = new ManagementObjectSearcher(managementScope, sqDfsrReplicatedFolderInfo);
                mocDfsrReplicatedFolderInfo = mosDfsrReplicatedFolderInfo.Get();
            }

            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public String GetOutboundBacklogFileIdRecords()
            {
                Object returnObj = null;
                try
                {
                    
                    foreach (ManagementObject moDfsrReplicatedFolderInfo in mocDfsrReplicatedFolderInfo)
                    {
                        returnObj = moDfsrReplicatedFolderInfo.InvokeMethod("GetOutboundBacklogFileIdRecords", null);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return null;
                }
                return (String)returnObj;
            }
            public void Update()
            {
                mocDfsrReplicatedFolderInfo = mosDfsrReplicatedFolderInfo.Get();
            }
            /// <summary>
            /// Retrieves the number of files scheduled to be replicated to an outbound partner given its version vector.
            /// </summary>
            /// <returns>UInt32</returns>
            public UInt32 GetOutboundBacklogFileCount()
            {
                Object[] getVersionVectorArgs = { null };
                Object[] args = { null, null, null };
                UInt32 returnLargest = 0;

                try
                {
                    foreach (ManagementObject moDfsrReplicatedFolderInfo in mocDfsrReplicatedFolderInfo)
                    {
                        UInt32 monitorStatusGVV     = (UInt32)moDfsrReplicatedFolderInfo.InvokeMethod("GetVersionVector", getVersionVectorArgs);
                        args[0]                     = getVersionVectorArgs[0];
                        UInt32 monitorStatus        = (UInt32) moDfsrReplicatedFolderInfo.InvokeMethod("GetOutboundBacklogFileCount", args);
                        if (returnLargest < (UInt32)args[1]) { returnLargest = (UInt32)args[1]; }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return 0;
                }
                return returnLargest;
            }
            /// <summary>
            /// Starts the Conflict and Deleted folder cleanup process.
            /// </summary>
            /// <returns>Boolean</returns>
            public Boolean CleanupConflictDirectory()
            {
                Object returnObj = null;
                try
                {
                    foreach (ManagementObject moDfsrReplicatedFolderInfo in mocDfsrReplicatedFolderInfo)
                    {
                        returnObj = moDfsrReplicatedFolderInfo.InvokeMethod("CleanupConflictDirectory", null);
                        if( (UInt32)returnObj != 0 ) {
                            throw new DfsrException((UInt32)returnObj);
                        }
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return false;
                }
                return true;
            }
            /// <summary>
            /// Applies a fence to a specific file or folder.
            /// An operation that protects a file so that the version with the highest fence will win during a conflict resolution.
            /// </summary>
            /// <param name="path"></param>
            /// <returns>Boolean</returns>
            public Boolean Fence(Byte Mode,String FilePath, Boolean IsRecursive)
            {
                ManagementClass managementClass = new System.Management.ManagementClass();
                object[] methodArgs = { Mode, FilePath, IsRecursive };
                

                return true;
            }
            /// <summary>
            /// Retrieves the list of files that are scheduled to be replicated to the outbound partner given its version vector.
            /// </summary>
            /// <returns></returns>
            public String GetVersionVector()
            {
                try
                {
                    foreach (ManagementObject moDfsrReplicatedFolderInfo in mocDfsrReplicatedFolderInfo)
                    {
                        Object returnObj = moDfsrReplicatedFolderInfo.InvokeMethod("GetVersionVector", null);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                }
                return "temp";
            }
            public void Get()
            {
                arrayDfsrReplicatedFolderInfo = new System.Collections.ArrayList();
                DfsrReplicatedFolderInfo _DfsrReplicatedFolderInfo = new DfsrReplicatedFolderInfo();
                try
                {
                    foreach (ManagementObject moDfsrReplicatedFolderInfo in mocDfsrReplicatedFolderInfo)
                    {
                        if (moDfsrReplicatedFolderInfo["ReplicatedFolderGuid"] != null)
                        {
                            _DfsrReplicatedFolderInfo.ReplicatedFolderGuid = (String)moDfsrReplicatedFolderInfo["ReplicatedFolderGuid"];
                        }
                        if (moDfsrReplicatedFolderInfo["ReplicatedFolderName"] != null)
                        {
                            _DfsrReplicatedFolderInfo.ReplicatedFolderName = (String)moDfsrReplicatedFolderInfo["ReplicatedFolderName"];
                        }
                        if (moDfsrReplicatedFolderInfo["MemberName"] != null)
                        {
                            _DfsrReplicatedFolderInfo.MemberName = (String)moDfsrReplicatedFolderInfo["MemberName"];
                        }
                        if (moDfsrReplicatedFolderInfo["LastConflictCleanupTime"] != null)
                        {
                            _DfsrReplicatedFolderInfo.LastConflictCleanupTime = (String)moDfsrReplicatedFolderInfo["LastConflictCleanupTime"];
                        }
                        if (moDfsrReplicatedFolderInfo["CurrentStageSizeInMb"] != null)
                        {
                            _DfsrReplicatedFolderInfo.CurrentStageSizeInMb = (UInt32)moDfsrReplicatedFolderInfo["CurrentStageSizeInMb"];
                        }
                        if (moDfsrReplicatedFolderInfo["CurrentConflictSizeInMb"] != null)
                        {
                            _DfsrReplicatedFolderInfo.CurrentConflictSizeInMb = (UInt32)moDfsrReplicatedFolderInfo["CurrentConflictSizeInMb"];
                        }
                        if (moDfsrReplicatedFolderInfo["State"] != null)
                        {
                            _DfsrReplicatedFolderInfo.State = (Byte)moDfsrReplicatedFolderInfo["State"];
                        }
                        if (moDfsrReplicatedFolderInfo["LastErrorCode"] != null)
                        {
                            _DfsrReplicatedFolderInfo.LastErrorCode = (UInt32)moDfsrReplicatedFolderInfo["LastErrorCode"];
                        }
                        if (moDfsrReplicatedFolderInfo["LastErrorMessageId"] != null)
                        {
                            _DfsrReplicatedFolderInfo.LastErrorMessageId = (UInt32)moDfsrReplicatedFolderInfo["LastErrorMessageId"];
                        }
                        arrayDfsrReplicatedFolderInfo.Add(_DfsrReplicatedFolderInfo);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    //System.Windows.Forms.MessageBox.Show( error );
                    Console.Error.WriteLine(error);
                }
            }
        }
        /// <summary>
        /// This class provides details about the files and folders that were 
        /// moved to the Conflict and Deleted folder due to conflicting updates.
        /// </summary>
        public class ConflictInfo
        {
            private System.Management.ManagementScope managementScope;
            public struct DfsrConflictInfo
            {
                public String GVsn;
                public String Uid;
                public String FileName;
                public UInt32 FileAttributes;
                public UInt32 ConflictFileCount;
                public UInt64 ConflictSizeInBytes;
                public String ConflictTime;
                public String ConflictPath;
                public Byte ConflictType;
                public String ReplicatedFolderGuid;
                public String ReplicationGroupGuid;
                public String MemberGuid;
                public String PartnerGuid;
            }
            public ConflictInfo(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
            }
            /// <summary>
            /// Deletes this conflict from the conflict directory along with its record in the manifest.
            /// </summary>
            /// <returns>Boolean</returns>
            public Boolean Delete()
            {
                return false;
            }
        }
        /// <summary>
        /// This class provides access to ID Table records. 
        /// The ID Table has a record for each file and folder known to DFSR. 
        /// In addition, it keeps records of the deleted content.
        /// </summary>
        public class IdRecordInfo
        {
            private System.Management.ManagementScope managementScope;
            private String queryDfsrIdRecordInfo = "SELECT * FROM DfsrIdRecordInfo";
            private System.Collections.ArrayList arrayDfsrIdRecordInfo;
            private System.Management.SelectQuery sqDfsrIdRecordInfo;
            private System.Management.ManagementObjectSearcher mosDfsrIdRecordInfo;
            private System.Management.ManagementObjectCollection mocDfsrIdRecordInfo;
            public struct DfsrIdRecordInfo
            {
                public String Uid;
                public Byte Flags;
                public UInt32 Attributes;
                public String GVsn;
                public String Usn;
                public String ParentUid;
                public UInt64 Fid;
                public String Volume;
                public String Fence;
                public String Clock;
                public String CreateTime;
                public String UpdateTime;
                public String FileHash;
                public String FileName;
                public UInt32 Index;
                public String ReplicatedFolderGuid;
                public String FullFilePath;
            }
            public IdRecordInfo(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqDfsrIdRecordInfo = new System.Management.SelectQuery(queryDfsrIdRecordInfo);
                mosDfsrIdRecordInfo = new ManagementObjectSearcher(managementScope, sqDfsrIdRecordInfo);
                mocDfsrIdRecordInfo = mosDfsrIdRecordInfo.Get();
            }
            public void Get()
            {
                arrayDfsrIdRecordInfo = new System.Collections.ArrayList();
                DfsrIdRecordInfo _DfsrIdRecordInfo = new DfsrIdRecordInfo();
                try
                {
                    foreach (ManagementObject moDfsrIdRecordInfo in mocDfsrIdRecordInfo)
                    {
                        if (moDfsrIdRecordInfo["Uid"] != null)
                        {
                            _DfsrIdRecordInfo.Uid = (String)moDfsrIdRecordInfo["Uid"];
                        }
                        if (moDfsrIdRecordInfo["Flags"] != null)
                        {
                            _DfsrIdRecordInfo.Flags = (Byte)moDfsrIdRecordInfo["Flags"];
                        }
                        if (moDfsrIdRecordInfo["Attributes"] != null)
                        {
                            _DfsrIdRecordInfo.Attributes = (UInt32)moDfsrIdRecordInfo["Attributes"];
                        }
                        if (moDfsrIdRecordInfo["GVsn"] != null)
                        {
                            _DfsrIdRecordInfo.GVsn = (String)moDfsrIdRecordInfo["GVsn"];
                        }
                        if (moDfsrIdRecordInfo["Usn"] != null)
                        {
                            _DfsrIdRecordInfo.Usn = (String)moDfsrIdRecordInfo["Usn"];
                        }
                        if (moDfsrIdRecordInfo["ParentUid"] != null)
                        {
                            _DfsrIdRecordInfo.ParentUid = (String)moDfsrIdRecordInfo["ParentUid"];
                        }
                        if (moDfsrIdRecordInfo["Fid"] != null)
                        {
                            _DfsrIdRecordInfo.Fid = (UInt64)moDfsrIdRecordInfo["Fid"];
                        }
                        if (moDfsrIdRecordInfo["Volume"] != null)
                        {
                            _DfsrIdRecordInfo.Volume = (String)moDfsrIdRecordInfo["Volume"];
                        }
                        if (moDfsrIdRecordInfo["Fence"] != null)
                        {
                            _DfsrIdRecordInfo.Fence = (String)moDfsrIdRecordInfo["Fence"];
                        }
                        if (moDfsrIdRecordInfo["Clock"] != null)
                        {
                            _DfsrIdRecordInfo.Clock = (String)moDfsrIdRecordInfo["Clock"];
                        }
                        if (moDfsrIdRecordInfo["CreateTime"] != null)
                        {
                            _DfsrIdRecordInfo.CreateTime = (String)moDfsrIdRecordInfo["CreateTime"];
                        }
                        if (moDfsrIdRecordInfo["UpdateTime"] != null)
                        {
                            _DfsrIdRecordInfo.UpdateTime = (String)moDfsrIdRecordInfo["UpdateTime"];
                        }
                        if (moDfsrIdRecordInfo["FileHash"] != null)
                        {
                            _DfsrIdRecordInfo.FileHash = (String)moDfsrIdRecordInfo["FileHash"];
                        }
                        if (moDfsrIdRecordInfo["FileName"] != null)
                        {
                            _DfsrIdRecordInfo.FileName = (String)moDfsrIdRecordInfo["FileName"];
                        }
                        if (moDfsrIdRecordInfo["Index"] != null)
                        {
                            _DfsrIdRecordInfo.Index = (UInt32)moDfsrIdRecordInfo["Index"];
                        }
                        if (moDfsrIdRecordInfo["ReplicatedFolderGuid"] != null)
                        {
                            _DfsrIdRecordInfo.ReplicatedFolderGuid = (String)moDfsrIdRecordInfo["ReplicatedFolderGuid"];
                        }
                        _DfsrIdRecordInfo.FullFilePath = GetFullFilePath(moDfsrIdRecordInfo);
                        arrayDfsrIdRecordInfo.Add(_DfsrIdRecordInfo);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    //System.Windows.Forms.MessageBox.Show( error );
                    Console.Error.WriteLine(error);
                }
            }
            /// <summary>
            /// Retrieves the complete path to the file or folder.
            /// </summary>
            /// <returns>String</returns>
            private String GetFullFilePath(ManagementObject moDfsrIdRecordInfo)
            {
                object[] methodArgs = { null };
                try
                {
                    object returnObj = moDfsrIdRecordInfo.InvokeMethod("GetFullFilePath", methodArgs);
                    if ((UInt32)returnObj != 0)
                    {
                        throw new DfsrException((UInt32)returnObj);
                    }
                    return (String)methodArgs[0];
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    Console.Error.WriteLine(error);
                    return "null";
                }
                return "null";
            }
        }
        /// <summary>
        /// This class provides statistical and operational information 
        /// for each replication group member hosted on the local computer.
        /// </summary>
        public class LocalMemberInfo
        {
            private System.Management.ManagementScope managementScope;
            public struct DfsrLocalMemberInfo
            {
                public String MemberGuid;
                public String MemberName;
                public String ReplicationGroupGuid;
                public String ReplicationGroupName;
                public Byte State;
                public String LastConfigChangeTime;
                public UInt32 LastErrorCode;
                public UInt32 LastErrorMessageId;
            }
            public LocalMemberInfo(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
            }
        }
        /// <summary>
        /// This class provides statistical and operational information 
        /// for each volume that has or had replicated folders.
        /// </summary>
        public class VolumeInfo
        {
            private String queryDfsrVolumeInfo = "SELECT * FROM DfsrVolumeInfo";
            private System.Collections.ArrayList arrayDfsrVolumeInfo;
            private System.Management.SelectQuery sqDfsrVolumeInfo;
            private System.Management.ManagementScope managementScope;
            private System.Management.ManagementObjectSearcher mosDfsrVolumeInfo;
            private System.Management.ManagementObjectCollection mocDfsrVolumeInfo;
            public struct DfsrVolumeInfo
            {
                public String VolumeGuid;
                public UInt64 VolumeSerialNumber;
                public String VolumePath;
                public String DatabaseGuid;
                public Byte State;
                public String LastConfigChangeTime;
                public UInt32 LastErrorCode;
                public UInt32 LastErrorMessageId;
            }
            public VolumeInfo(System.Management.ManagementScope managementScope)
            {
                this.managementScope = managementScope;
                sqDfsrVolumeInfo = new System.Management.SelectQuery(queryDfsrVolumeInfo);
                mosDfsrVolumeInfo = new ManagementObjectSearcher(managementScope, sqDfsrVolumeInfo);
                mocDfsrVolumeInfo = mosDfsrVolumeInfo.Get();
            }
            public void Get()
            {
                arrayDfsrVolumeInfo = new System.Collections.ArrayList();
                DfsrVolumeInfo _DfsrVolumeInfo = new DfsrVolumeInfo();
                try
                {
                    foreach (ManagementObject moDfsrVolumeInfo in mocDfsrVolumeInfo)
                    {
                        if (moDfsrVolumeInfo["VolumeGuid"] != null)
                        {
                            _DfsrVolumeInfo.VolumeGuid = (String)moDfsrVolumeInfo["VolumeGuid"];
                        }
                        if (moDfsrVolumeInfo["VolumePath"] != null)
                        {
                            _DfsrVolumeInfo.VolumePath = (String)moDfsrVolumeInfo["VolumePath"];
                        }
                        if (moDfsrVolumeInfo["State"] != null)
                        {
                            _DfsrVolumeInfo.State = (Byte)moDfsrVolumeInfo["State"];
                        }
                        if (moDfsrVolumeInfo["LastConfigChangeTime"] != null)
                        {
                            _DfsrVolumeInfo.LastConfigChangeTime = (String)moDfsrVolumeInfo["LastConfigChangeTime"];
                        }
                        if (moDfsrVolumeInfo["LastErrorCode"] != null)
                        {
                            _DfsrVolumeInfo.LastErrorCode = (UInt32)moDfsrVolumeInfo["LastErrorCode"];
                        }
                        if (moDfsrVolumeInfo["LastErrorMessageId"] != null)
                        {
                            _DfsrVolumeInfo.LastErrorMessageId = (UInt32)moDfsrVolumeInfo["LastErrorMessageId"];
                        }
                        arrayDfsrVolumeInfo.Add(_DfsrVolumeInfo);
                    }
                }
                catch (Exception e)
                {
                    string error = "Message: " + e.Message + " Source: " + e.Source;
                    //System.Windows.Forms.MessageBox.Show( error );
                    Console.Error.WriteLine(error);
                }
            }
        }
    }
}
